package com.linedata.acq.metier.norme.fmb;

import java.util.ArrayList;
import java.util.List;

import oracle.forms.jdapi.Block;
import oracle.forms.jdapi.FormModule;
import oracle.forms.jdapi.Item;
import oracle.forms.jdapi.JdapiIterator;
import oracle.forms.jdapi.LOV;
import oracle.forms.jdapi.RecordGroup;

public class GLOV_FMB {

	private FormModule	fmb;

	public GLOV_FMB(FormModule fmb) {
		this.fmb = fmb;
	}

	private String	error	= "";

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error += error;
	}

	/**
	 * EKFR005_1 :: Le nom des records group est « nom_du_block »
	 * _ « nom_du_champ »_LOVn
	 * 
	 * @param fmb
	 */
	public void nomRecordGroup() {
		// TODO item name & test number is integer

		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();

		// LOV lov = null;
		for (JdapiIterator recordGroups = fmb.getRecordGroups(); recordGroups.hasNext();) {
			RecordGroup recordGroup = (RecordGroup) recordGroups.next();

			list1.add(recordGroup.getName());

			for (JdapiIterator blocks = ((FormModule) fmb).getBlocks(); blocks.hasNext();) {
				Block block = (Block) blocks.next();

				if (recordGroup.getName().startsWith(block.getName())) {

					for (JdapiIterator items = block.getItems(); items.hasNext();) {
						Item item = (Item) items.next();

						if (recordGroup.getName().startsWith(block.getName() + "_" + item.getName() + "_LOV")) {

							list2.add(recordGroup.getName());
						}
					}
				}
			}
		}

		for (int i = 0; i < list2.size(); i++) {
			String str1 = (String) list2.get(i);
			for (int j = 0; j < list1.size(); j++) {
				String str2 = (String) list1.get(j);
				int result = str1.compareTo(str2);
				if (result == 0)
					list1.remove(j);
			}
		}

		for (String l1 : list1) {
			this.setError("\tEKFR005_1 : " + l1 + "\n");
		}

	}

	/**
	 * EKFR006_1 :: Le nom des LOV est « nom_du_block »
	 * _ « nom_du_champ »_LOVn
	 * 
	 * @param fmb
	 */
	public void nomLOV() {
		// TODO item name & test number is integer

		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();

		// LOV lov = null;
		for (JdapiIterator lovs = fmb.getLOVs(); lovs.hasNext();) {
			LOV lov = (LOV) lovs.next();

			list1.add(lov.getName());

			for (JdapiIterator blocks = ((FormModule) fmb).getBlocks(); blocks.hasNext();) {
				Block block = (Block) blocks.next();

				if (lov.getName().startsWith(block.getName())) {

					for (JdapiIterator items = block.getItems(); items.hasNext();) {
						Item item = (Item) items.next();

						String lovName = block.getName() + "_" + item.getName() + "_LOV";
						if (lovName.length() < 26 || lovName.length() < 27) {

							if (lov.getName().startsWith(block.getName() + "_" + item.getName() + "_LOV")) {
								list2.add(lov.getName());
							}
						} else {
							if (lov.getName().startsWith(block.getName() + "_" + "_LOV")) {
								list2.add(lov.getName());
							}
						}
					}
				}
			}
		}

		for (int i = 0; i < list2.size(); i++) {
			String str1 = (String) list2.get(i);
			for (int j = 0; j < list1.size(); j++) {
				String str2 = (String) list1.get(j);
				int result = str1.compareTo(str2);
				if (result == 0)
					list1.remove(j);
			}
		}

		for (String l1 : list1) {
			this.setError("\tEKFR006_1 : " + l1 + "\n");
		}
	}

	@Override
	public String toString() {
		return this.getError();
	}
}
